package middleware

import (
	"net/http"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// Cross config
func Cross() gin.HandlerFunc {

	config := cors.DefaultConfig()

	config.AllowOrigins = []string{"*"}
	config.AllowHeaders = []string{
		"X-Requested-With",
		"Accept",
		"Authorization",
		"Selected-Role",
		"Origin",
		"Access-Control-Allow-Origin",
		"Manage-Type",
		"user-id",
		"Content-Length",
		"Content-Type",
	}
	config.AllowCredentials = true

	return cors.New(config)
}

// Options .
func Options() gin.HandlerFunc {

	return func(c *gin.Context) {

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}

		c.Next()
	}
}
